package com.example.fina2202.day09;

public class StringBinarySearch {
    public static void main(String[] args) {
        String[] ary = {"amy","jack","rose","tom","tony","张三","李四"};
        String target = "张三";
        int index = search(ary,target);
        System.out.println(index);
    }

    public static int search(String[] ary,String target){
        int start=0,end=ary.length-1,middle;

        while (start<=end){
            middle = (end-start)/2+start;
            //判断target与middle元素是否相等
            if (target.compareTo(ary[middle])==0)
                return middle;
            else if (target.compareTo(ary[middle])>0)
                start = middle+1;
            else
                end = middle-1;
        }
        return -1;
    }
}
